<?php
namespace app\models;
use ac\component\tool\Tool;
use app\models\Basic;

use Yii;

class SendAuction extends Basic
{
    
    public static function tableName()
    {
        return '{{%pj8_send_auction}}';
    }

    public function rules()
    {
        return [
            ['member_id', 'integer', 'message' => 'member_id必须为数字，或数字字符串', 
                     'max'=>18446744073709551615, 'tooBig' => 'member_id最大存储18446744073709551615'],
            ['goods_id', 'integer', 'message' => 'goods_id必须为数字，或数字字符串', 
                     'max'=>18446744073709551615, 'tooBig' => 'goods_id最大存储18446744073709551615'],
            ['name', 'required', 'message' => 'name不能为空', 'on' => 'add'],
            ['name', 'string', 'message' => 'name必须是字符串',
                                'min' => 1, 'tooShort' => 'name最短1位',
                                'max' => 16, 'tooLong' => 'name最长16位'],
            ['send_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['send_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'send_time非yyyy-MM-dd HH:mm:ss格式'],    
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加*/
    public function addSendAuction($data)
    {
        // P($data);
        $this->scenario = 'add';
        if(!isset($data['SendAuction']['add_time']) or empty($data['SendAuction']['add_time'])){
            $data['SendAuction']['add_time'] = date('Y-m-d H:i:s', time());
        }
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                $id = $this->getPrimaryKey();
                return $id;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    $app_id 
    */
    public function modifySendAuction($data, $id)
    {
        // P($data);
        $sendAuction = self::find()->where('id = :id', [':id' => $id])->one();
        // P($sendAuction);
        // P($data);
        if(!is_null($sendAuction)){
            if($this->load($data) and $this->validate()){
                if(isset($this->name)){
                    $sendAuction->name = $this->name;
                }
                if(isset($this->member_id)){
                    $sendAuction->member_id = $this->member_id;
                }
                if(isset($this->goods_id)){
                    $sendAuction->goods_id = $this->goods_id;
                }
                if(isset($this->name)){
                    $sendAuction->name = $this->name;
                }
                if(isset($this->send_time)){
                    $sendAuction->send_time = $this->send_time;
                }
                if($sendAuction->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    /*获取内容的会员信息*/
    public function getMember()
    {
        $info = $this->hasOne(Member::className(), ['member_id' => 'member_id'])
                    ->select(['{{%pj8_member}}.member_id', 'nickname']);
        return $info;
    }

    /*获取内容的鉴品信息*/
    public function getGoods()
    {
        $info = $this->hasOne(Goods::className(), ['goods_id' => 'goods_id'])
                    ->select(['{{%pj8_goods}}.goods_id', 'name']);
        return $info;
    }

    
}